﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace PHP
{
	public partial class dto : Form
	{
		public dto()
		{
			InitializeComponent();
		}
		private bool kiemTra()
		{
			for (int i = 0; i < dg.RowCount - 1; i++)
			{
				for (int j = i + 1; j < dg.RowCount; j++)
				{
					if (dg[1, i].Value.Equals(dg[1, j].Value))
					{
						MessageBox.Show("Lỗi lặp lại");
						dg.CurrentCell = dg.Rows[j].Cells[1];
						dg.BeginEdit(true);



						return false;
					}
				}
			}
			return true;
		}
		private string vietHoa(string str)
		{
			//var ci = new CultureInfo("vi-VI");
			string tmp = "";
			char[] s = str.ToArray();
			if (s.Length > 0)
			{
				s[0] = char.ToUpper(s[0]);
			}
			foreach (var i in s)
			{
				tmp += i;
			}
			return tmp.ToString();
		}
		private void btToaCode_Click(object sender, EventArgs e)
		{
			if (!kiemTra()) return;// nếu sai thì dừng lại không làm nữa

			int n = dg.RowCount - 1;
			string str = "class " + txClass.Text.Trim() + "{";
			str += "\n";
			for (int i = 0; i < n; i++)
			{
				//các biến
				str += "\t private $" + dg[1, i].Value.ToString().Trim() + ";";
				str += "\n";
			}
			str += "\n";
			//construct
			if (ckConstruct.Checked)
			{
				str += "public function __construct(";
				for (int i = 0; i < n - 1; i++)
				{
					str +="$"+ dg[1, i].Value.ToString() + "=\"\",";
				}
				str += dg[1, n - 1].Value.ToString() + "=\"\")";
				for (int i = 0; i < n; i++)
				{
					str += "\n\t\t$this->" + dg[1, i].Value.ToString().Trim() + "=" + "$" + dg[1, i].Value.ToString().Trim() + ";";
				}
				str += "\n\t}";
			}
			str += "\n";

			// JSon
			if (ckGetJson.Checked)
			{
				str += "public function getJson(){";
				str += "\n\t   $item = array(";
				for (int i = 0; i < n - 1; i++)
				{
					str += "\n\t\t'" + dg[1, i].Value.ToString().Trim() + "'=>$this->" + dg[1, i].Value.ToString().Trim() + ",";
				}
				str += "\n\t\t'" + dg[1, n - 1].Value.ToString().Trim() + "'=>$this->" + dg[1, n - 1].Value.ToString().Trim() ;
				str += "\n\t );";
				str += "\n\t return $item;";
				str += "\n}";
			}
			str += "\n";
			// hàm set
			for (int i = 0; i < n; i++)
			{
				if (dg[2, i].Value != null)
				{
					if (bool.Parse(dg[2, i].Value.ToString()))
					{
						str += "public function Set" +vietHoa(dg[1, i].Value.ToString().Trim() ) + "($" + dg[1, i].Value.ToString().Trim() + "){";
						str += "\n\t\t $this->" + dg[1, i].Value.ToString().Trim() + " = $" + dg[1, i].Value.ToString().Trim() + ";";
						str += "\n\t}\n";
					}
				}
			}

			str += "\n";

			// hàm get
			for (int i = 0; i < n; i++)
			{
				if (dg[2, i].Value != null)
				{
					if (bool.Parse(dg[2, i].Value.ToString()))
					{
						str += "public function Get" + vietHoa( dg[1, i].Value.ToString().Trim() )+ "(){";
						str += "\n\t\t return $this->" + dg[1, i].Value.ToString().Trim() + ";";
						str += "\n\t}\n";
					}
				}
			}
			//cuối class
			str += "\n}";

			txt1.Text = str.ToString();

		}

		private void dg_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			int row = dg.CurrentCell.RowIndex;
			dg[0, row].Value = row + 1;
			//set
			dg[2, row].Value = true;
			//get
			dg[3, row].Value = true;

		}

		private void dto_Load(object sender, EventArgs e)
		{

		}

		private void btCopy_Click(object sender, EventArgs e)
		{
		}


	}
}